package top.kaoshanji.p2ch10a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 代码清单-p2ch10a-314页a：在父类构造方法中调用子类方法的错误示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/2 17:21
 */
public class BadConstructor {

    private static final Logger logger = LogManager.getLogger(BadConstructor.class);

    public static void main(String[] args) {
        logger.info("...代码清单-p2ch10a-314页a：在父类构造方法中调用子类方法的错误示例...");

        BadConstructor bc = new BadConstructor();
        bc.test();
    }

    public void test() {
        Child child = new Child(5);
    }

}

class Parent {

    public Parent() {
        // 构造器里的方法不要被子类覆写
        int average = 30 / getCount();
    }

    // 被子类覆写了..
    protected int getCount() {
        return 4;
    }
}

class Child extends Parent {

    private int count;

    public Child(int count) {
        this.count = count;
    }

    @Override
    public int getCount() {
        return count;
    }
}


